'''
Created on Jan 25, 2010

@author: madsk
'''

from PyQt4 import QtCore
from imageops import *
import logging

class ImageWorker(QtCore.QThread):
    def __init__(self, image = None, operation = None, args = None):
        super(ImageWorker, self).__init__()
        self._image = image
        self._operation = operation
        self._args = args
        self._success = True

    def run(self):
        logger = logging.getLogger('ImageWorker')
        self._success = True

        # Switch out depending on the operation.
        if self._operation == 'sharpen':
            try:
                self._image = sharpen(self._image, *self._args)
            except Exception, e:
                logger.error("Error performing image sharpening.", exc_info=True)
                self._success = False

        elif self._operation == 'blur':
            try:
                self._image = blur(self._image, *self._args)
            except Exception, e:
                logger.error("Error performing image blurring.", exc_info=True)
                self._success = False

        elif self._operation == 'color':
            try:
                self._image = color(self._image, *self._args)
            except Exception, e:
                logger.error("Error performing color adjustment.", exc_info=True)
                self._success = False

        elif self._operation == 'contrast':
            try:
                self._image = contrast(self._image, *self._args)
            except Exception, e:
                logger.error("Error performing contrast adjustment.", exc_info=True)
                self._success = False

        elif self._operation == 'brightness':
            try:
                self._image = brightness(self._image, *self._args)
            except Exception, e:
                logger.error("Error performing brightness adjustment.", exc_info=True)
                self._success = False

        elif self._operation == 'fliphorizontal':
            try:
                self._image = fliphorizontal(self._image)
            except Exception, e:
                logger.error("Error performing horizontal flip.", exc_info=True)
                self._success = False

        elif self._operation == 'flipvertical':
            try:
                self._image = flipvertical(self._image)
            except Exception, e:
                logger.error("Error performing vertical flip.", exc_info=True)
                self._success = False
            
    def image(): #@NoSelf
        def fget(self):
            return self._image
        def fset(self, value):
            self._image = value
        def fdel(self):
            del self._image
        return locals()
    image = property(**image())

    def operation(): #@NoSelf
        def fget(self):
            return self._operation
        def fset(self, value):
            self._operation = value           
        def fdel(self):
            del self._operation
        return locals()
    operation = property(**operation())
        
    def args(): #@NoSelf
        def fget(self):
            return self._args
        def fset(self, value):
            self._args = value
        def fdel(self):
            del self._args
        return locals()
    args = property(**args())    
    
    def success(): #@NoSelf
        def fget(self):
            return self._success
        return locals()
    success = property(**success())